package com.hjk.common.utils.http;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Date;

/**
 * @author huangjunkai
 * @version 1.0
 * @date 2020/11/11 19:35
 */
@Slf4j
public class HttpUtils {

    @SneakyThrows
    public RequestEntity<Void> getRequestEntityByGet(String url) {
        return RequestEntity.get(new URI(url))
                .accept(MediaType.APPLICATION_JSON_UTF8)
                .build();
    }
    @SneakyThrows
    public RequestEntity<Void> getRequestEntityByGet(String url, String token) {
        return RequestEntity.get(new URI(url))
                .accept(MediaType.APPLICATION_JSON_UTF8)
                .header(HttpHeaders.AUTHORIZATION, token)
                .build();
    }

    @SneakyThrows
    public RequestEntity<Void> getRequestEntityByGet(String url, HttpHeaders headers) {
        return RequestEntity.get(new URI(url))
                .accept(MediaType.APPLICATION_JSON_UTF8)
                .headers(headers)
                .build();
    }

    @SneakyThrows
    public <T> RequestEntity<T> getRequestEntityPost(String url, T body) {
        return RequestEntity.post(new URI(url))
                .accept(MediaType.APPLICATION_JSON_UTF8)
                .body(body);
    }

}
